// pages/shopping_cart/shopping_cart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    totalPrice: 0, 
    haslist:false,
    bool: true,
    hots:false,
    hot: [
      {
        "imgs": "https://gitee.com/yuexiayuhui/fruitday_Optimization/raw/master/images/%E9%A6%96%E9%A1%B5/%E5%88%97%E8%A1%A8/Screenshot_2018-06-07-19-58-55-346_com.tencent.mm_08.png",
        "title": "能量巨果爆浆过瘾",
        "price": "69",
        "number": "4kg",
        "plunge": "明日达",
        "num":1
      },
      {
        "imgs": "https://gitee.com/yuexiayuhui/fruitday_Optimization/raw/master/images/%E9%A6%96%E9%A1%B5/%E5%88%97%E8%A1%A8/Screenshot_2018-06-07-19-58-55-346_com.tencent.mm_08.png",
        "title": "能量巨果爆浆过瘾",
        "price": "70",
        "number": "6kg",
        "plunge": "明日达",
        "num": 1
      }
      ,
      {
        "imgs": "https://gitee.com/yuexiayuhui/fruitday_Optimization/raw/master/images/%E9%A6%96%E9%A1%B5/%E5%88%97%E8%A1%A8/Screenshot_2018-06-07-19-58-55-346_com.tencent.mm_08.png",
        "title": "能量巨果爆浆过瘾",
        "price": "70",
        "number": "6kg",
        "plunge": "明日达",
        "num": 1
      }
      ,
      {
        "imgs": "https://gitee.com/yuexiayuhui/fruitday_Optimization/raw/master/images/%E9%A6%96%E9%A1%B5/%E5%88%97%E8%A1%A8/Screenshot_2018-06-07-19-58-55-346_com.tencent.mm_08.png",
        "title": "能量巨果爆浆过瘾",
        "price": "70",
        "number": "6kg",
        "plunge": "明日达",
        "num": 1
      }
      ,
      {
        "imgs": "https://gitee.com/yuexiayuhui/fruitday_Optimization/raw/master/images/%E9%A6%96%E9%A1%B5/%E5%88%97%E8%A1%A8/Screenshot_2018-06-07-19-58-55-346_com.tencent.mm_08.png",
        "title": "能量巨果爆浆过瘾",
        "price": "70",
        "number": "6kg",
        "plunge": "明日达",
        "num": 1
      }
      ,
      {
        "imgs": "https://gitee.com/yuexiayuhui/fruitday_Optimization/raw/master/images/%E9%A6%96%E9%A1%B5/%E5%88%97%E8%A1%A8/Screenshot_2018-06-07-19-58-55-346_com.tencent.mm_08.png",
        "title": "能量巨果爆浆过瘾",
        "price": "70",
        "number": "6kg",
        "plunge": "明日达",
        "num": 1
      }
    ]
  },
  /*加*/
  add:function(e){
      const  index=e.currentTarget.dataset.index;
      let hot = this.data.hot;
      let num=hot[index].num;
      num++;
      hot[index].num=num;
      this.setData({
        hot: hot
      })
      this.getTotalPrice();
  },
  /*减*/
 jian:function(e){
   const index = e.currentTarget.dataset.index;
   let hot = this.data.hot;
   let num = hot[index].num;
   if(num<=1){
     return false
   }
   num--;
   hot[index].num = num;
   this.setData({
     hot: hot
   })
   this.getTotalPrice();
 },
 /* 总价 */
 getTotalPrice() {
   let hot = this.data.hot;                  // 获取购物车列表
   let total = 0;
   for (let i = 0; i < hot.length; i++) {         // 循环列表得到每个数据
     if (hot[i].selected) {                   // 判断选中才会计算价格
       total += hot[i].num * hot[i].price;     // 所有价格加起来
     }
   }
   this.setData({                                // 最后赋值到data中渲染到页面
     hot: hot,
     totalPrice: total.toFixed(2)
   });
 },
 //绑定单选
 bindCheckbox: function (e) {
   var idx = e.currentTarget.dataset.index;
   var hot = this.data.hot;
   var status = this.data.hots
   var selected = hot[idx].selected;
   hot[idx].selected = !selected;
   
   this.setData({
     hots: !status
   });
   this.getTotalPrice();
 },
//绑定多选
  bindSelectAll: function (e) {
   var selectedAllStatus = this.data.selectedAllStatus;
   var hot = this.data.hot;
   selectedAllStatus = !selectedAllStatus;
   for (var i = 0; i < hot.length; i++) {
     hot[i].selected = selectedAllStatus;
   }
   this.setData({
     hots: hot,
     selectedAllStatus: selectedAllStatus
   });
   this.getTotalPrice();
 },
  del:function(e){
    console.log(e)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})